/*
	Appendix A Figure A1
	
	Main figure broken out by prejudice measures. 

*/


use data_study_1.dta, clear

// Set omitted categories
	gen zero_race = 0
	label var zero_race "White"
	gen zero_sex = 0
	label var zero_sex "Male"
	gen zero_eco = 0
	label var zero_eco "Maintain investment in energy"
	gen zero_aa = 0
	label var zero_aa "Not shown policy"
	
// Set regression variables
	loc reg_race "cand_black zero_race"
	loc reg_sex "cand_female zero_sex"
	loc reg_issues "cand_policy_abort1 cand_policy_abort2 cand_policy_tax1 cand_policy_tax2"
	loc reg_issues "`reg_issues' cand_policy_health1 cand_policy_health2 cand_policy_eco1 zero_eco"
	loc reg_issues_aa "cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 zero_aa"
		
// Interactive Terms for Black Candidate X Affirmative Action
	gen noracepolicy = cand_policy_aa1 == 0 & cand_policy_aa2 == 0 & cand_policy_aa3 == 0	
	label var noracepolicy "Not shown position"
	gen black_aa1 = cand_policy_aa1*cand_black
	gen black_aa2 = cand_policy_aa2*cand_black
	gen black_aa3 = cand_policy_aa3*cand_black
	gen white_aa1 = cand_policy_aa1*cand_white
	gen white_aa2 = cand_policy_aa2*cand_white
	gen white_aa3 = cand_policy_aa3*cand_white
	gen black_aa0 = noracepolicy*cand_black
	gen white_aa0 = noracepolicy*cand_white
	label var black_aa0 "Black X No Position"
	label var white_aa0 "White X No Position"
	label var black_aa1 "Black X Expand (race)"
	label var black_aa2 "Black X Keep as is"
	label var black_aa3 "Black X Replace (class)"
	label var white_aa1 "White X Expand (race)"
	label var white_aa2 "White X Keep as is"
	label var white_aa3 "White X Replace (class)"
	replace white_aa0 = 0
	label var noracepolicy "Not shown position"
	loc int "white_aa0 black_aa0 white_aa1 white_aa2 white_aa3 black_aa1 black_aa2 black_aa3"
	

//============================================= Figure A2. Prejudice
	gen out_fair_bwdiff = out_fair_black - out_fair_white
	
	
	foreach spec in rr ep {	
		if "`spec'" == "rr" {
			loc gr1 "High Racial Resentment"
			loc gr2 "Low Racial Resentment"
			loc tit "Racial Resentment Measure"
			rename resent_binary rr_binary
		}
	
		if "`spec'" == "ep" {
			loc gr1 "High Explicit Prejudice"
			loc gr2 "Low Explicit Prejudice"
			loc tit "Explicit Prejudice Measure"
			rename overt_binary ep_binary
		}
		
		reg out_ideo_7 `reg_sex' `reg_issues' `int' cand_age if `spec'_binary == 1, robust
		eststo ideo_`spec'
		reg out_ideo_7 `reg_sex' `reg_issues' `int' cand_age if `spec'_binary == 0, robust
		eststo ideo_n`spec'
		
		reg out_priority_sj `reg_sex' `reg_issues' `int' if `spec'_binary == 1, robust
		eststo `spec'_out_priority_sj
		reg out_priority_sj `reg_sex' `reg_issues' `int' if `spec'_binary == 0, robust
		eststo `spec'n_out_priority_sj
		
		reg out_fair_bwdiff `reg_sex' `reg_issues' `int' if `spec'_binary == 1, robust
		eststo `spec'_fair
		reg out_fair_bwdiff `reg_sex' `reg_issues' `int' if `spec'_binary == 0, robust
		eststo `spec'n_fair

		coefplot (ideo_`spec', label(`gr2') mc(maroon) ciopts(color(maroon) lw(med))) ///
				 (ideo_n`spec', label(`gr2') mc(gs4) ciopts(color(gs4) lw(med))) ///
				 , bylabel("{bf:(a) Ideological Liberalness}") || ///
				(`spec'_out_priority_sj, label(`gr1') mc(maroon) ciopts(color(maroon) lw(med))) ///
				(`spec'n_out_priority_sj, label(`gr2') mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(b) Prioritize Social Justice}") || ///
				(`spec'_fair, label(`gr1') mc(maroon) ciopts(color(maroon) lw(med))) ///
				(`spec'n_fair, label(`gr2') mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(c) Favor Black over White Constituents}") ///
			|| , drop(_cons `reg_sex' `reg_issues' cand_age) omitted baselevels ms(c) msize(med) ///
			xline(0, lc(black)) ylabel(,labsize(small))  ///
			subtitle(, bcolor(white) color(black) size(vsmall)) ///
			byopts(row(1) ///
				t1title("{bf: `tit'}", size(small))) ///
			xtitle("Marginal Effects (Scale 0 to 1)", size(vsmall)) xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
			legend(order(2 "`gr1'" 4 "`gr2'") size(vsmall)) ///
			headings(white_aa0 = "{bf: No Affirmative Action}" ///
				white_aa1 = "{bf: White X Affirmative Action}" ///
				black_aa1 = "{bf: Black X Affirmative Action}", labsize(small)) ///
				saving(fig_`spec'.gph, replace)
			
	}
	
	graph combine fig_rr.gph fig_ep.gph, row(2) imargin(vsmall)

	graph export figure_A1.png, as(png) replace
	
	
	
//----------------- Table A1 (Demographics)

// Study 1
	use data_study_1.dta, clear

	keep  r_* hhi_* male female edu_* reg_*
	drop r_pid r_other

	gen total = 1
	d, varlist
	foreach v of varlist `r(varlist)' {
		//loc var = subinstr("`v'", "x_", "", .)
		gen n_`v' = `v' 
		gen m_`v' = `v'
	}
		
	collapse (sum) n_* (mean) m_*
	gen i = .
	reshape long n_ m_, i(i) j(x) s
	drop i
		
	
	replace n = . if x == "r_age"
	replace m = . if x == "total"
	
	gen var = x
	replace var = "Age" if x == "r_age"
	replace var = "Democrat" if x == "r_dem"
	replace var = "Republican" if x == "r_gop"
	replace var = "Independent" if x == "r_ind"
	replace var = "Other" if x == "r_oth"
	replace var = "< High School" if x == "edu_nohs"
	replace var = "High School Diploma" if x == "edu_hs"
	replace var = "Some College" if x == "edu_somecol"
	replace var = "Associate's" if x == "edu_2yr"
	replace var = "Bachelor's" if x == "edu_ba"
	replace var = "Postgraduate" if x == "edu_postgrad"
	replace var = "< $30,000" if x == "hhi_1"
	replace var = "$30,000 - $59,999" if x == "hhi_2"
	replace var = "$60,000 - $99,999" if x == "hhi_3"
	replace var = "$100,00 - $200,000" if x == "hhi_4"
	replace var = "$250,000+" if x == "hhi_5"
	replace var = "Prefer not to say" if x == "hhi_6"
	replace var = "Northeast" if x == "reg_ne"
	replace var = "Midwest" if x == "reg_mw"
	replace var = "South" if x == "reg_s"
	replace var = "West" if x == "reg_w"
	replace var = "White" if x == "r_white"
	replace var = "Black" if x == "r_black"
	replace var = "Asian" if x == "r_asian"
	replace var = "Hispanic" if x == "r_hispanic"
	replace var = proper(var)
	
	rename n n_study1
	rename m m_study1
	
	tempfile temp
	save `temp', replace
	

// Study 2
	insheet using study_2_raw.csv, comma name clear

	keep if acq_pass == "1" | acq_identity == "Because he left his ID"
		
	// Qualtrics demographics
	gen r_age = 2021 - real(dem_birth)
	
	// Respondent Race
	gen r_white = dem_race == "White"
	gen r_black = dem_race == "Black or African American"
	gen r_asian = dem_race == "Asian"
	gen r_hispanic = dem_race == "Hispanic or Latino"
	gen r_other = missing(r_white) & missing(r_black) & missing(r_asian) & missing(r_hispanic)
	
	// Party
	gen r_dem = dem_pid == "Democrat"
	gen r_gop = dem_pid == "Republican"
	gen r_ind = dem_pid == "Independent"
	gen r_oth = dem_pid == "Other"
	
	label var r_dem "Respondent PID, Democrats (Incl. Leaners)"
	label var r_ind "Respondent PID, Pure Independents"
	label var r_gop "Respondent PID, Republicans (Incl. Leaners)"
	label var dem_pid "Respondent PID from Qualtrics"

	gen male = gender == "1"
	gen female = gender == "2"

	gen temp = real(hhi)
	drop hhi
	rename temp hhi
	gen hhi_1 = inrange(hhi, 1, 4) // <30k
	gen hhi_2 = inrange(hhi, 5, 10) // 30-60k
	gen hhi_3 = inrange(hhi, 11, 18) // 60-100k
	gen hhi_4 = inlist(hhi, 19, 20, 21, 22) // 100-200k
	gen hhi_5 = inlist(hhi, 23, 24) // 200+
	gen hhi_6 = hhi == -3105 // prefer not say

	gen temp = real(educ)
	drop educ
	rename temp educ
	gen edu_nohs = educ == 1
	gen edu_hs = inlist(educ, 2, 3)
	gen edu_somecol = educ == 4
	gen edu_2yr = educ == 5
	gen edu_ba = educ == 6
	gen edu_postgrad = inlist(educ, 7, 8)

	gen temp = real(region)
	drop region
	rename temp region
	gen reg_ne = region == 1
	gen reg_mw = region == 2
	gen reg_s = region == 3
	gen reg_w = region == 4
		
	keep  r_* hhi_* male female edu_* reg_*
	drop r_other r_id

	gen total = 1
	d, varlist
	foreach v of varlist `r(varlist)' {
		//loc var = subinstr("`v'", "x_", "", .)
		gen n_`v' = `v' 
		gen m_`v' = `v'
	}
		
	collapse (sum) n_* (mean) m_*
	gen i = .
	reshape long n_ m_, i(i) j(x) s
	drop i
	
	replace n = . if x == "r_age"
	replace m = . if x == "total"

	gen var = x
	replace var = "Age" if x == "r_age"
	replace var = "Democrat" if x == "r_dem"
	replace var = "Republican" if x == "r_gop"
	replace var = "Independent" if x == "r_ind"
	replace var = "Other" if x == "r_oth"
	replace var = "< High School" if x == "edu_nohs"
	replace var = "High School Diploma" if x == "edu_hs"
	replace var = "Some College" if x == "edu_somecol"
	replace var = "Associate's" if x == "edu_2yr"
	replace var = "Bachelor's" if x == "edu_ba"
	replace var = "Postgraduate" if x == "edu_postgrad"
	replace var = "< $30,000" if x == "hhi_1"
	replace var = "$30,000 - $59,999" if x == "hhi_2"
	replace var = "$60,000 - $99,999" if x == "hhi_3"
	replace var = "$100,00 - $200,000" if x == "hhi_4"
	replace var = "$250,000+" if x == "hhi_5"
	replace var = "Prefer not to say" if x == "hhi_6"
	replace var = "Northeast" if x == "reg_ne"
	replace var = "Midwest" if x == "reg_mw"
	replace var = "South" if x == "reg_s"
	replace var = "West" if x == "reg_w"
	replace var = "White" if x == "r_white"
	replace var = "Black" if x == "r_black"
	replace var = "Asian" if x == "r_asian"
	replace var = "Hispanic" if x == "r_hispanic"
	replace var = proper(var)
	
	rename n n_study2
	rename m m_study2
	

	cap drop _m
	merge 1:1 x using `temp'

	gen order = .
	replace order = 0.1 if x == "r_white"
	replace order = 0.2 if x == "r_black"
	replace order = 0.3 if x == "r_asian"
	replace order = 0.4 if x == "r_hispanic"
	replace order = 1 if x == "r_age"
	replace order = 2 if x == "female"
	replace order = 3 if x == "male"
	replace order = 4 if x == "r_dem"
	replace order = 5 if x == "r_gop"
	replace order = 6 if x == "r_ind"
	replace order = 7 if x == "r_oth"
	replace order = 8 if x == "edu_nohs"
	replace order = 9 if x == "edu_hs"
	replace order = 10 if x == "edu_somecol"
	replace order = 11 if x == "edu_2yr"
	replace order = 12 if x == "edu_ba"
	replace order = 13 if x == "edu_postgrad"
	replace order = 14 if x == "hhi_1"
	replace order = 15 if x == "hhi_2"
	replace order = 16 if x == "hhi_3"
	replace order = 17 if x == "hhi_4"
	replace order = 18 if x == "hhi_5"
	replace order = 19 if x == "hhi_6"
	replace order = 20 if x == "reg_ne"
	replace order = 21 if x == "reg_mw"
	replace order = 22 if x == "reg_s"
	replace order = 23 if x == "reg_w"
	replace order = 24 if x == "total"
	
	order var n_study1 m_study1 n_study2 m_study2
	sort order 
	drop x _m order
	list

	outsheet using table_a1.csv, comma replace

